//
//  SquarePostsDetailsTableViewCell.m
//  安好
//
//  Created by rlp on 14-11-21.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "SquarePostsDetailsTableViewCell.h"

@implementation SquarePostsDetailsTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        /**
         头像
         */
        _headImage = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, ((kScreenWidth == 414)?40*1.2:40), ((kScreenWidth == 414)?40*1.2:40))];
        _headImage.layer.masksToBounds = YES;
        _headImage.layer.cornerRadius = 3.0;
        _headImage.layer.borderWidth = 0.0;
        [self.contentView addSubview:_headImage];
        float headImageHight = _headImage.frame.origin.y+_headImage.frame.size.height;
        
        _tagText = [[UILabel alloc]initWithFrame:CGRectZero];
        [self.contentView addSubview:_tagText];
        _headerTitle = [[UILabel alloc]init];
        [self.contentView addSubview:_headerTitle];
        
        _timeImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_headImage.frame.origin.x+self.headImage.frame.size.width+15, self.headImage.frame.origin.y+self.headImage.frame.size.height-12, ((kScreenWidth == 414)?12*1.2:12), ((kScreenWidth == 414)?12*1.2:12))];
        _timeImageView.image = [UIImage imageNamed:@"广场_时间"];
        [self.contentView addSubview:_timeImageView];
        
        
        
        _rclabel = [[UILabel alloc]init];
        [self.contentView addSubview:_rclabel];
        _imageListView = [[ImageListView alloc]init];
        
        
        
        
//        _headerTitle = [[UILabel alloc]initWithFrame:CGRectMake(_headImage.frame.origin.x+_headImage.frame.size.width+15, _headImage.frame.origin.y, kScreenWidth-(_headImage.frame.size.width)-15*2-_tagText.frame.size.width, 18) text:self.headerTitle.text font:[UIFont systemFontOfSize:LABEL_FONT_SIZE_TITLE] textColor:TITLE_ARTICLE_TEXTFONT_COLOR backgroundColor:[UIColor clearColor] numberOfLines:1 lineSpacing:0];
        
        
        self.frame =CGRectMake(0, 0,kScreenWidth,headImageHight);

        
        
        
//        self.headerTimeText = [[UILabel alloc]init];
//        self.tagText = [[UILabel alloc]init];
//        self.tableView =[[SquarePostsDetailsReplylTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
//        self.quoteArray = [[NSMutableArray alloc]initWithCapacity:10];
//        
//        self.imageListimages = [[NSMutableArray alloc]initWithCapacity:10];
//        
//        self.imageListView = [[ImageListView alloc]init];
//        self.contentStr = [[NSString alloc]init];

        
        

    
    }
    return self;
}


-(void)setTagText:(UILabel *)tagText{


    
    
}


-(void)setModelData:(NSMutableDictionary *)modelData{
    _modelData = modelData;
    if (self.contentView.subviews.count>0) {
        for (UIView *view in self.contentView.subviews) {
            [view removeFromSuperview];
        }
    }
    
    
    _headImage = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, ((kScreenWidth == 414)?40*1.2:40), ((kScreenWidth == 414)?40*1.2:40))];
    _headImage.layer.masksToBounds = YES;
    _headImage.layer.cornerRadius = 3.0;
    _headImage.layer.borderWidth = 0.0;
    /**
     cell头像加点击事件
     */
    UITapGestureRecognizer *headImageViewtapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cellSubheadImageViewClick:)];
    [_headImage setUserInteractionEnabled:YES];
    [_headImage addGestureRecognizer:headImageViewtapGesture];
    
    
    [_headImage setImageURLStr:[modelData objectForKey:@"headImage"] placeholder:[UIImage imageNamed:@"默认头像"]];
    [self.contentView addSubview:_headImage];
    float headImageHight = _headImage.frame.origin.y+_headImage.frame.size.height;
    
    _tagText = [[UILabel alloc]initWithFrame:CGRectZero];
    [self.contentView addSubview:_tagText];
    _headerTitle = [[UILabel alloc]init];
    [self.contentView addSubview:_headerTitle];
    
    _timeImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_headImage.frame.origin.x+self.headImage.frame.size.width+15, self.headImage.frame.origin.y+self.headImage.frame.size.height-12, ((kScreenWidth == 414)?12*1.2:12), ((kScreenWidth == 414)?12*1.2:12))];
    _timeImageView.image = [UIImage imageNamed:@"广场_时间"];
    [self.contentView addSubview:_timeImageView];
    self.frame =CGRectMake(0, 0,kScreenWidth,headImageHight);


    /**
     *  标记
     */
    [_tagText removeFromSuperview];
    if ([modelData objectForKey:@"tagText"]) {
        _tagText = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth-15-((kScreenWidth == 414)?40*1.2:40), _headImage.frame.origin.y, ((kScreenWidth == 414)?40*1.2:40), 18) text:[modelData objectForKey:@"tagText"] font:[UIFont systemFontOfSize:DVSWITCH_YEXT_FONT] textColor:[UIColor whiteColor] backgroundColor:[ClassMethodsViewController colorWithHexString:FLAG_BACKGROUNDCOLOR_16] numberOfLines:1 lineSpacing:0];
        _tagText.textAlignment = NSTextAlignmentCenter;
        _tagText.layer.masksToBounds = YES;
        _tagText.layer.cornerRadius = 2.0;
        _tagText.layer.borderWidth = 0.0;
        _tagText.frame = CGRectMake(kScreenWidth-15-_tagText.frame.size.width, _tagText.frame.origin.y, _tagText.frame.size.width+2*2, _tagText.frame.size.height);
        [self.contentView addSubview:_tagText];
    }
    self.frame =CGRectMake(0, 0,kScreenWidth,self.frame.size.height);

    /**
     *  名字
     */
    [_headerTitle removeFromSuperview];
    if ([modelData objectForKey:@"headerTitle"]) {
    _headerTitle = [[UILabel alloc]initWithFrame:CGRectMake(_headImage.frame.origin.x+_headImage.frame.size.width+15, _headImage.frame.origin.y, kScreenWidth-(_headImage.frame.size.width)-15*2-_tagText.frame.size.width, 18) text:[modelData objectForKey:@"headerTitle"] font:[UIFont systemFontOfSize:LABEL_FONT_SIZE_TITLE] textColor:TITLE_ARTICLE_TEXTFONT_COLOR backgroundColor:[UIColor clearColor] numberOfLines:1 lineSpacing:0];
        [self.contentView addSubview:_headerTitle];
    }
    /**
     *  时间
     */
    [_headerTimeText removeFromSuperview];
    if ([modelData objectForKey:@"headerTimeText"]) {
        _headerTimeText = [[UILabel alloc]initWithFrame:CGRectMake(_timeImageView.frame.origin.x+_timeImageView.frame.size.width+2, _timeImageView.frame.origin.y, _timeImageView.frame.origin.x+_timeImageView.frame.size.width+2-15*2, ((kScreenWidth == 414)?12*1.2:12)) text:[modelData objectForKey:@"headerTimeText"] font:[UIFont systemFontOfSize:LABEL_FONT_SIZE_TIME] textColor:TIME_COLOR backgroundColor:[UIColor clearColor] numberOfLines:1 lineSpacing:0];
        [self.contentView addSubview:_headerTimeText];
    }
    
    /**
     *  内容
     */
    CGFloat contentHight = 10;
    if ([modelData objectForKey:@"contentStr"]) {
        
        _rclabel = [[UILabel alloc]initWithFrame:CGRectMake(15,(self.headImage.frame.origin.y+self.headImage.frame.size.height+15),kScreenWidth-15*2,0) text:[modelData objectForKey:@"contentStr"] font:[UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13] textColor:TITLE_ARTICLE_CONTENTFONT_COLOR backgroundColor:[UIColor clearColor] numberOfLines:0 lineSpacing:5];
        
//        NSString *str = [HtmlString transformString:[modelData objectForKey:@"contentStr"]];

//        _rclabel = [[RCLabel alloc]initWithFrame:CGRectMake(10,(self.headImage.frame.origin.y+self.headImage.frame.size.height+15),kScreenWidth-10*2,0)];
//            _rclabel.delegate =self;
//            _rclabel.sizeDelegate = self;
//        [_rclabel setFont:[UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13]];
//        RCLabelComponentsStructure *componentsDS = [RCLabel extractTextStyle:str];
//        _rclabel.componentsAndPlainText = componentsDS;
//        CGSize optimalSize = [_rclabel optimumSize:YES];   //计算图文混排后的高度
//        _rclabel.frame=CGRectMake(10,_rclabel.frame.origin.y,kScreenWidth-10*2, optimalSize.height+5);
//        _rclabel.lineBreakMode =NSLineBreakByCharWrapping;
//        _rclabel.backgroundColor = [UIColor clearColor];
//        _rclabel.textColor =[UIColor orangeColor];
//        _rclabel.textColor = TITLE_ARTICLE_CONTENTFONT_COLOR;
        

        contentHight = _rclabel.frame.size.height+10;
        
        [self.contentView addSubview:_rclabel];

    }
    self.frame =CGRectMake(0, 0,kScreenWidth,self.frame.size.height+contentHight);


    /**
     *  图片
     */
    [_imageListView removeFromSuperview];
    if ([modelData objectForKey:@"imageListimages"]) {
        
        
        _imageListView = [[ImageListView alloc]initWithFrame:CGRectMake(15, self.frame.origin.y+self.frame.size.height+10, self.frame.size.width-15*2, 0) imageViews:[modelData objectForKey:@"imageListimages"]];
        _imageListView.delegate = self;
        _imageListView.tag =self.index;
        [self.contentView addSubview:self.imageListView];
        CGFloat imageListViewHight = _imageListView.frame.size.height+20;
        self.frame =CGRectMake(0, 0,kScreenWidth,self.frame.size.height+imageListViewHight);
        
        
    }
    /**
     *  评论列表
     */
    [_tableView removeFromSuperview];
    if ([modelData objectForKey:@"quote"]) {

        
        
        if ([[modelData objectForKey:@"quote"] count]!=0) {
            
            _tableView = [[SquarePostsDetailsReplylTableView alloc]initWithFrame:CGRectMake(10,self.frame.origin.y+self.frame.size.height,kScreenWidth-10*2,self.tableView.frame.size.height) style:UITableViewStylePlain];
            _tableView.index = self.index;
            
            _tableView.arr = [[NSMutableArray alloc]initWithArray:[modelData objectForKey:@"quote"]];
            
            
            
            UIView *contentBottomLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0,_tableView.frame.size.width,0.5)];
            
            
            contentBottomLine.backgroundColor =[ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
            
            [_tableView setTableHeaderView:contentBottomLine];
            [self.contentView addSubview:_tableView];

            CGFloat tableViewHight = _tableView.contentSize.height+1+2;

            self.frame =CGRectMake(0, 0,kScreenWidth,self.frame.size.height+tableViewHight);
            _tableView.backgroundColor = [UIColor redColor];


        }else{
            
        }

    }

    
    MyNSLog("%f", self.frame.size.height);
    
    
    CGFloat moreButtonHight = 0;

    
    if ([[modelData objectForKey:@"quotecount"] integerValue]>2) {
        if ([[modelData objectForKey:@"quote"] count]<=2) { //无查看更多
            UIButton *button = [[UIButton alloc]init];
            button.titleLabel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13];
            [button setTitleColor:[ClassMethodsViewController colorWithHexString:NACIGATIONBAR_BACKGROUNDCOLOR_16] forState:UIControlStateNormal];
            [button setTitle:@"查看更多" forState:UIControlStateNormal];
            [button sizeToFit];
            [button addTarget:self action:@selector(gengduo:) forControlEvents:UIControlEventTouchUpInside];
            moreButtonHight=button.size.height;
            button.tag = self.index;
            
            button.frame = CGRectMake(kScreenWidth-button.frame.size.width-15, self.frame.size.height-15, button.frame.size.width, button.frame.size.height);
            [self.contentView addSubview:button];
            MyNSLog("%f", self.frame.size.height);

            self.frame =CGRectMake(0, 0,kScreenWidth,self.frame.size.height+moreButtonHight-10);

        }else{
            UIButton *button = [[UIButton alloc]init];
            button.titleLabel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13];
            [button setTitleColor:[ClassMethodsViewController colorWithHexString:NACIGATIONBAR_BACKGROUNDCOLOR_16] forState:UIControlStateNormal];
            [button setTitle:@"收起" forState:UIControlStateNormal];
            [button sizeToFit];
            [button addTarget:self action:@selector(gengduo:) forControlEvents:UIControlEventTouchUpInside];
            moreButtonHight=button.size.height;
            button.tag = self.index;
            
            button.frame = CGRectMake(kScreenWidth-button.frame.size.width-15, self.frame.size.height-15, button.frame.size.width, button.frame.size.height);
            [self.contentView addSubview:button];
            MyNSLog("%f", self.frame.size.height);
            
            self.frame =CGRectMake(0, 0,kScreenWidth,self.frame.size.height+moreButtonHight-10);
        }
        
        
    }else{
        self.frame =CGRectMake(0, 0,kScreenWidth,self.frame.size.height+moreButtonHight);

    }
    
    
    

    
    /**
     *  线条的处理
     */
    
    UIView *bottomLineView = [[UIView alloc]initWithFrame:CGRectMake(5, self.frame.size.height-1,kScreenWidth-5, 0.5)];
    
    bottomLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
    [self.contentView addSubview:bottomLineView];



}

/**
 *  点击cell头像
 *
 *  @param tapGestureRecognizer <#tapGestureRecognizer description#>
 */
-(void)cellSubheadImageViewClick:(UITapGestureRecognizer *)tapGestureRecognizer{
    UIImageView *imageview = (UIImageView*)tapGestureRecognizer.view;
    imageview.tag = self.index;
//    int tags =imageview.tag;
    
    [self.delegate squarePostsDetailsTableViewWithImageView:imageview didSelectCellAtIndex:self.index];

}

-(IBAction)gengduo:(UIButton *)sender{
    [self.delegate squarePostsDetailsTableViewWithButton:sender didSelectCellAtIndex:self.index];

}

-(void)imageViewsIndex:(NSInteger)index subImageIndex:(NSInteger)imageIndex{
    MyNSLog("cell%d,%d",index,imageIndex);
    
    
    NSMutableArray *arrimages = [[NSMutableArray alloc ]initWithArray:[self.modelData objectForKey:@"imageListimages"]];
    ImageListView *imageListView = (ImageListView*)[self viewWithTag:index];
    
    UIImageView *imageView = (UIImageView *)[imageListView viewWithTag:imageIndex];
    
    
    
    int count = arrimages.count;
    // 1.封装图片数据
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i<count; i++) {
        // 替换为中等尺寸图片
//        NSString *url = arrimages[i];
        MJPhoto *photo = [[MJPhoto alloc] init];
        NSString *url =nil;
        NSRange range;
        range = [arrimages[i] rangeOfString:@"@"];
        if (range.location != NSNotFound) {
            url = [arrimages[i] substringToIndex:range.location];
        }else{
            url = arrimages[i];
        }
        photo.url = [NSURL URLWithString:url]; // 图片路径
        photo.srcImageView = imageView; // 来源于哪个UIImageView
        [photos addObject:photo];
    }
    
    // 2.显示相册
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.currentPhotoIndex = imageIndex; // 弹出相册时显示的第一张图片是？
    browser.photos = photos; // 设置所有的图片
    [browser show];
    

}

#pragma mark: RCLabelDelegate
-(void)RCLabel:(id)RCLabel didSelectLinkWithURL:(NSString *)url
{
    
    MyNSLog("url======%@",url);
}
#pragma mark: RCLabelSizeDelegate

- (void)RCLabel:(id)RCLabel didChangedSize:(CGSize)size{
    MyNSLog("%f,%f",size.width,size.height);
}

//- (void)drawRect:(CGRect)rect
//{
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    
//    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
//    CGContextFillRect(context, rect);
//    
//    //    //上分割线，
//    //    CGContextSetStrokeColorWithColor(context, [ClassMethodsViewController colorWithHexString:@"#e2e2e2"].CGColor);
//    //    CGContextStrokeRect(context, CGRectMake(5, -1, rect.size.width - 10, 1));
//    
//    //下分割线
//    CGContextSetStrokeColorWithColor(context, [ClassMethodsViewController colorWithHexString:@"#e2e2e2"].CGColor);
//    CGContextStrokeRect(context, CGRectMake(0, rect.size.height, rect.size.width, 1));
//}


@end
